New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

new-object

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

new-object

Creates an object from an array of entries, in imitation of the Map constructor.

  • 3.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.8K
decreased by-8.48%
Maintainers
1
Weekly downloads
 
Created
Source

new-object

Creates an object from an array of entries.

The Map constructor lets you specify initial keys and values via an iterable collection (e.g. an array) of key-value pairs, whereas the Object constructor does not. This simple module fills that gap, approximating the ECMA-defined behavior for the Map constructor but for Objects.

In short, you can think of the new-object module as doing the opposite of Object.entries():

newObject(Object.entries({a: 1, b: 2})) // {a: 1, b: 2}

Remember that, unlike Maps, Objects can only use strings and symbols as keys.

Installation

Requires Node.js 6.0.0 or above.

npm i new-object

API

The module exports a single function.

Parameters

  1. entries (iterable): The key-value pairs for the object.
  2. Optional: Object argument:
    • throwIfEquivKeys (Error, string, or boolean): Set this to throw an error if entries contains keys that would be considered duplicates in the context of an object. For example, a Map can have keys that are objects, but those keys will all likely evaluate to [object Object] if made keys in an object. Similarly, a Map can have distinct 1 (number) and '1' (string) keys, but these would be considered the same in an object context.

Return Value

Returns a plain object containing entries.

Example

const newObject = require('new-object')

const entries = [['a', 1], ['b', 2]]

// What was possible for a Map...
const map = new Map(entries)
map.get('a') // 1

// ...is now also possible for an Object:
const obj = newObject(entries)
obj.a // 1
  • construct-map: Like this module, except it can also construct Maps and other key/value collections.

Keywords

FAQs

Package last updated on 18 Aug 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc